package com.liumuxian.base.service.impl;

import com.liumuxian.base.dao.IntegralDataDao;
import com.liumuxian.base.pojo.IntegralData;
import com.liumuxian.base.service.IntegralDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import util.VagueUtil;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;

@Service
@Transactional
public class IntegralDataServiceImpl implements IntegralDataService {
    @Autowired
    VagueUtil<IntegralData> vagueUtil;
    @Autowired
    IntegralDataDao integralDataDao;
    @Autowired
    IdWorker idWorker;
    @Override
    public void deletes(String id) {
        integralDataDao.deleteById(id);
    }

    @Override
    public Page<IntegralData> findAll(Integer pagesize, Integer pagenumber, IntegralData integralData)throws Exception {
        HashMap<String,Object> hm=new HashMap<>();
        Method[] sourceMethods = integralData.getClass().getMethods();
        for(int i=0;i<sourceMethods.length;i++){
            if(sourceMethods[i].getName().startsWith("get")){
                String lsName = sourceMethods[i].getName().substring(3);   // 属性
                Object loValue = sourceMethods[i].invoke(integralData, null);  // 值
                if(!lsName.toLowerCase().equals("class")){
                    hm.put(lsName.toLowerCase(),loValue);
                }
            }
        }
        Specification<IntegralData> specification = vagueUtil.createSpecification(hm);
        PageRequest pageRequest =  PageRequest.of(pagenumber-1, pagesize);
        return integralDataDao.findAll(specification,pageRequest);
    }

    @Override
    public void updates(IntegralData integralData) {
        integralData.setUpdatedate(new Date());
        integralDataDao.saveAndFlush(integralData);
    }

    @Override
    public void save(IntegralData integralData) {
        integralData.setId(idWorker.nextId()+"");
        integralData.setCreatdata(new Date());
        integralData.setUpdatedate(new Date());
        integralDataDao.save(integralData);
    }

    @Override
    public IntegralData updatetopage(String id) {
        return integralDataDao.getOne(id);
    }

    @Override
    public void saveorders(Integer integral, String userid, String id) {
        IntegralData integralData=new IntegralData();
        integralData.setId(idWorker.nextId()+"");
        integralData.setUpdatedate(new Date());
        integralData.setCreatdata(new Date());
        integralData.setUid(userid);
        integralData.setState(1);
        integralData.setOrdersid(id);
        integralData.setIntegral(0-integral);
        integralData.setIntegraltype("1207902472782352384");
        integralDataDao.save(integralData);
    }
}
